package org.example.teacher.Service;

import org.example.teacher.Dao.UserDao;
import org.example.teacher.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(User user) {
        String sql="insert into user values(null,'"+user.getUsername() +"','"+user.getPassword()+"','0')";
        return jdbcTemplate.update(sql);
    }


    @Override
    public int delete(User user) {
        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public User findOne(User user) {
        String sql = "select * from user where username like '%"+user.getUsername()+"%'";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public List<User> findAll() {
        return jdbcTemplate.query("select * from user",new BeanPropertyRowMapper<User>(User.class));
    }
}
